﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.Match
{
    public partial class NeedMatchList : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            string strURL = HttpContext.Current.Request.Url.AbsolutePath;
            bool bok = Comm.ifhasright(strUserID, strURL);
            if (!bok)
            {
                Response.Write("你无权访问此页面");
                Response.End();
            }

            if (!Page.IsPostBack)
            {
                //Comm.BindDll(ref dropTerm, "TermType", true, "");
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "All";
                liAll.Value = "All";
                dropProd.Items.Insert(0, liAll);

                //Comm.BindDll(ref dropEntity, "Entity", true, "");
                //DataSet dsEntity = EntityDao.getEntityList("N");
                //dropEntity.DataSource = dsEntity;
                //dropEntity.DataTextField = "entity_name";
                //dropEntity.DataValueField = "entity_name";
                //dropEntity.DataBind();
                ////ListItem liAllEntity = new ListItem();
                ////liAllEntity.Text = "All";
                ////liAllEntity.Value = "All";
                ////dropEntity.Items.Insert(0, liAllEntity);
                //dropEntity.Items.FindByValue(strEntity).Selected = true;
                //dropEntity.Enabled = false;

                //txtcreatedatebegin.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
                //txtcreatedateend.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            bindGrid();
        }

        private void bindGrid()
        {
            try
            {
                string strWhere = " where status IN ('IV','EV') ";
                if (txtCardNo.Text.Trim() != "")
                    strWhere += " and d.card_no ='" + txtCardNo.Text.Trim() + "'";
                if (txtCpartyName.Text.Trim() != "")
                    strWhere += " and c.cparty_name like '%" + txtCpartyName.Text.Trim() + "%'";
                if (dropProd.SelectedIndex > 0)
                {
                    strWhere += " and d.prod_code = '" + dropProd.SelectedItem.Value + "'";
                }

                if (txtcreatedatebegin.Text != "" && txtcreatedateend.Text != "")
                    strWhere += " and convert(date,d.submit_date) between '" + txtcreatedatebegin.Text.Trim() + "' and '" + txtcreatedateend.Text.Trim() + "'";

                strWhere += " ORDER BY deal_no ";
                DataSet ds = DealDao.getNeedMatch(strWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    GvData.DataSource = ds.Tables[0];
                    GvData.DataBind();
                }
                else
                {
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = (LinkButton)e.Row.Cells[1].Controls[1];
                lb.Attributes.Add("onclick", "javascript:window.open('/eCard/CardDetail.aspx?cardno=" + lb.Text + "');return false");

                string strStatus = GvData.DataKeys[e.Row.RowIndex].Values["status"].ToString(); //e.Row.Cells[4].Text;

                string if_isExtension = e.Row.Cells[11].Text;

                LinkButton lbAction = (LinkButton)e.Row.Cells[12].Controls[1];

                Label lbdealno = (Label)e.Row.Cells[0].Controls[1];                

                if (if_isExtension.Equals("Y"))
                {
                    //extension核实
                    if (strStatus.Equals("EV"))
                    {
                        lbAction.Text = "核实";
                        lbAction.Attributes.Add("onclick", "javascript: var s_height = screen.height - 75; window.open('match.aspx?type=EV&deal_no=" + lbdealno.Text + "', 'newwindow', 'width=' + screen.width + ',height=' +  s_height + ',top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');return false");
                    }
                    else
                    {
                        lbAction.Text = "";
                    }
                }
                else
                {
                    //添加核实
                    if (strStatus.Equals("IV"))
                    {
                        lbAction.Text = "核实";
                        lbAction.Attributes.Add("onclick", "javascript: var s_height = screen.height - 75; window.open('match.aspx?type=IV&deal_no=" + lbdealno.Text + "', 'newwindow', 'width=' + screen.width + ',height=' +  s_height + ',top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');return false;");
                    }
                    else
                    {
                        lbAction.Text = "";
                    }
                }
            }
        }
    }
}